package com.itheima.d1_ip;

import java.net.InetAddress;

//目标：掌握InetAddress类的使用
public class Demo01 {
    public static void main(String[] args) throws Exception {
        /**
         * 表示ip地址
         * 常用api：
         *      static InetAddress getLocalHost()：返回本主机的地址对象
         *      static InetAddress getByName(String host)：得到指定主机的IP地址对象，参数是域名或者IP地址
         *      String getHostName()：获取此IP地址的主机名
         *      String getHostAddress()：返回IP地址字符串
         *      boolean isReachable(int timeout)：在指定毫秒内连通该IP地址对应的主机，连通返回true
         */

        //1.获取本机的IP地址对象
        InetAddress ip1 = InetAddress.getLocalHost();
        System.out.println(ip1);

        //2.获取本机IP地址对象，根据ip字符串获取
        //InetAddress.getByName("127.0.0.1");
        InetAddress ip2 = InetAddress.getByName("localhost");
        System.out.println(ip2);

        //3.获取ip1的主机名
        String hostName1 = ip1.getHostName();
        System.out.println(hostName1);

        //4.获取ip2的地址字符串
        String hostAddress1 = ip1.getHostAddress();
        System.out.println(hostAddress1);

        //5.看是否和指定的ip(192.168.75.249)联通
        InetAddress ip3 = InetAddress.getByName("192.168.68.80");
        //InetAddress ip3 = InetAddress.getByName("www.baidu.com");
        boolean result = ip3.isReachable(3000);
        System.out.println(result);
    }
}
